﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:include value="/comm/header.jsp" />
<s:include value="/comm/menu.jsp" />
<div class="wrap1">
    	<div class="blank1"></div>
        <div class="outbox">
<!--1 start-->
			<div>
        		<div class="wrap_left">
                	<span class="tab7_tt">委托代购</span>
            	</div>
            	<div class="wrap_right">
            		<div class="rbox2">
						<div class="pbox6">
            				<ul>
                			<li class="bg1">&nbsp;</li>
                			<li class="s1"><span class="tt">1.&nbsp;&nbsp;确定订单信息</span></li>
                    		<li class="bg2">&nbsp;<li>
                   			<li class="s2"><span class="tt">2.&nbsp;&nbsp;托管代购款到代购岛平台</span></li>
                    		<li class="bg3">&nbsp;<li>
                    		<li class="s2"><span class="tt">3.&nbsp;&nbsp;经纪人发货</span></li>								
							<li class="bg3">&nbsp;</li>
                    		<li class="s2"><span class="tt">4.&nbsp;&nbsp;确定收货</span></li>
							<li class="bg3">&nbsp;</li>
                    		<li class="s2"><span class="tt">5.&nbsp;&nbsp;评价</span></li>
							<li class="bg4">&nbsp;</li>
                			</ul>
                        </div>
            		</div>
                    
                </div>
                <div class="clear"></div>
            </div>
            
            
            <div class="pbox7">
               <div class="tips4"><span style="float:right" class="span10"><a href="userAddrInit.htm" target="_blank" >管理收货地址</a></span>确认收货地址</div>
               <ul id="userAddrs">
               	 <s:iterator value="userAddrs" status="st">
               	 	<s:if test="#st.index == 0">
               	 		<li class="focus" id="mailTo<s:property value='addrId' />"><span>寄送至：</span><input name="addrRd" type="radio" value="<s:property value='addrId' />,<s:property value='userId' />,<s:property value='person' />,<s:property value='phone' />,<s:property value='address' />,<s:property value='postcode' />" checked /> <span class="span34"> <s:property value='mailTo'/></span><span>（默认地址）</span></li>
               	 	</s:if>
               	 	<s:else>
               	 		<li id="mailTo<s:property value='addrId' />"><span></span><input name="addrRd" type="radio" value="<s:property value='addrId' />,<s:property value='userId' />,<s:property value='person' />,<s:property value='phone' />,<s:property value='address' />,<s:property value='postcode' />" /><span><s:property value='mailTo'/></span><span class="set"><a defaultValue="<s:property value='addrId' />,<s:property value='userId' />" href="javascript:void(0)">设置为默认收货地址</a></span></li>
               	 	</s:else>
               	 </s:iterator>
               </ul>
			</div>
		<!--1 end -->
		<div class="pbox_address_click">
            <div class="link2"><a id="newAddress" href="javascript:void(0);">+ 新增收货地址</a></div>
        </div>
 		<div class="pbox7"><div class="tips4">订单确认</div></div>   
		<!--订单确认列表开始-->
        <div class="pbox8">
                <ul>
                	<li class="t1 tt">&nbsp;</li>
                    <li class="t2 tt"><span>名称</span></li>
                    <li class="t3 tt"><span>经纪人</span></li>
                    <li class="t4 tt"><span>价格</span></li>
                    <li class="t5 tt"><span>服务费</span></li>
                    <li class="t6 tt"><span>数量</span></li>
                    <li class="t7 tt"><span>小计</span></li>
                    <div class="clear"></div>
                </ul>
                <ul class="ttbox">
                	<li class="t1"><div class="pbox1_list_pic"><div><p><img src="<s:property value='product.images50[0]' />"/></p></div></div></li>
                    <li class="t2 t2_a"><span><s:property value="product.goodsName" /></span></li>
                    <li class="t3"><span><s:property value="product.user.nickName" /></span></li>
                    <li class="t4"><span class="span18" id="price">￥<s:property value="product.price" /></span></li>
                    <li class="t5"><span class="span19" id="serviceFee">￥<s:property value="product.serviceFee" /></span></li>
                    <li class="t6"><span><input type="text" id="buyNumber" name="buyNumber" class="input1" value="<s:property value='number' />"  size="1" /></span></li>
                    <li class="t7"><span class="span18" id="subTotal">￥<s:property value="subTotal" /></span></li>
                    <div class="clear"></div>
                </ul>
         </div>
 		<!--订单确认列表结束-->  
 		<div class="blank1"></div>
	 		<s:form method="post" action="confirm_order" id="confirmOrder">
	 					<input type="hidden" name="order.address.address" id="receiveAddress" />
	 					<input type="hidden" name="order.broker.userid" value="<s:property value='product.user.userid' />"  />
	                	<input type="hidden" name="order.address.phone" id="receivePhone" />
	                	<input type="hidden" name="order.address.postcode" id="receivePostcode" />
	                	<input type="hidden" name="order.address.person" id="receivePerson" />
			 			<input type="hidden" name="cid"  value="<s:property value='product.canId' />" />
			 			<input type="hidden" name="order.inviteId" value="<s:property value='iid' />" />
			 			<input type="hidden" name="order.simpleId" value="<s:property value='sid' />" />
			 			<input type="hidden" name="order.wantId" value="<s:property value='wid' />" />
			 			<input type="hidden" id="shippingCost" name="order.logisticCharge" />
			 			<s:hidden id="number" name="number" />
			 	
				<div class="pbox8">
	            	<div class="wrap_left">
	            		给经纪人留言：
	            		<input id="message" name="order.message" type="text" class="input1" size="40"/>
	            	</div>
	                <div class="wrap_right">
	                	<input type="hidden" name="firstCost" id="firstCost" value="<s:property value='product.logistics.price' />" />
	                	<input type="hidden" name="continuedCost" id="continuedCost" value="<s:property value='product.logistics.continuedPrice' />" />
	                	<strong>运费</strong><span class="span2" id="shippingCostSpan"></span>&nbsp;&nbsp;&nbsp;
	                </div>
	                <div class="clear"></div>
	               
	                <div class="but_submit" style="text-align:center; margin:30px 0">
	                	<s:a href='javascript:confirmOrder()'>提交订单</s:a>&nbsp;&nbsp;&nbsp;
	                </div>
	        	</div>
			</s:form>
        </div>
</div>
<s:include value="/comm/bottom.jsp"/>

<script type="text/javascript">
var lastCheckId = 0;
var lastDefaultId = 0;
var userId = "<s:property value='#session.user.userid' />";
var isLoad = false;
$(window).load(function(){
	calcShippingCost();
	
	var backUrl = '${requestScope.backUrl}';
	
	if ($("[name=addrRd]").length == 0) {
		$("#cancel").html("<a href='" + backUrl + "'>返回上一步</a>");
		$("#closeAddress").attr("href", backUrl);
		showDiv();
	}
	else {
		lastCheckId =  $("[name=addrRd]:checked").val().split(",")[0];
		lastDefaultId = lastCheckId;
		load();
		isLoad = true;
	}
	
	

});

function load() {
	$("[name=addrRd]").live("click", function() {
		selectAddress($(this).val().split(",")[0]);
	});
	$("#newAddress").click(function() {
		$("#cancel").html("<a href='javascript:closeDiv();'>取消</a>");
		showDiv();
	});
	$("[name=buyNumber]").each(function() {
		$(this).blur(function() {
			var number = $(this).val();
			if (!digitRegex.test(number)) {
				$(this).val(1);
				number = 1;
			}
			$("#number").val(number);
			var price = parseFloat($("#price").html().substring(1));
			var serviceFee = parseFloat($("#serviceFee").html().substring(1));
			var subTotal = ((price + serviceFee) * number).toFixed(2);
			$("#subTotal").html("￥" + subTotal);
			
			calcShippingCost();
		});
		$(this).focus(function() {
			var number = $(this).val();
			$(this).val(number);
			
		});
	});
	
	$("#userAddrs a").live("click", function() {
		setDefault($(this).attr("defaultValue"));
	});
}



function selectAddress(curCheckId) {
	$("#mailTo" + lastCheckId).removeClass("focus");
	$("#mailTo" + lastCheckId + " span:eq(0)").html("");
	$("#mailTo" + lastCheckId + " span:eq(1)").removeClass("span34");

	$("#mailTo" + curCheckId).addClass("focus");
	$("#mailTo" + curCheckId + " span:eq(0)").html("寄送至：");
	$("#mailTo" + curCheckId + " span:eq(1)").addClass("span34");
	
	lastCheckId = curCheckId;
}


function calcShippingCost() {
	var number = $("#buyNumber").val();
	var firstCost = parseFloat($("#firstCost").val());
	var continuedCost = parseFloat($("#continuedCost").val());
	var shippingCost = (firstCost + (number - 1) * continuedCost).toFixed(2);
	$("#shippingCost").val(shippingCost);
	$("#shippingCostSpan").html("￥" + shippingCost);
}

function setDefault(curDefaultValue) {
	var array = curDefaultValue.split(",");
	$.ajax({
			url:'setDefaultAddrForAjax.htm',
			type:'post',
			data:{"userAddr.addrId":array[0],"userAddr.userId":array[1]},
			success:function(json) {
				$("#mailTo" + lastDefaultId + " span:eq(2)").addClass("set");
				$("#mailTo" + lastDefaultId + " span:eq(2)").html("<a defaultValue='" + lastDefaultId + "," + array[1] + "' href='javascript:void(0)'>设置为默认收货地址</a>");
				$("#mailTo" + array[0] + " span:eq(2)").removeClass("set");
				$("#mailTo" + array[0] + " span:eq(2)").html("（默认地址）");
				lastDefaultId = array[0];
			}
		}
	);
}


function showDiv(){
var arrayPageSize   = getPageSize();//调用getPageSize()函数
var arrayPageScroll = getPageScroll();//调用getPageScroll()函数
document.getElementById('pbox_address').style.display='block';
document.getElementById('pbox_address').style.top = arrayPageScroll[1] + (arrayPageSize[3] - 35 - 400) / 2 + 'px';
document.getElementById('pbox_address').style.left = (arrayPageSize[0] - 20 - 750) / 2 + 'px';
document.getElementById('fixbg').style.display='block';
}


function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		sScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	// var pageHeight,pageWidth;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement
			&& document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	var pageWidth, pageHeight
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if (xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
	return arrayPageSize;
}
// 获取滚动条的高度
function getPageScroll() {
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('', yScroll)
	return arrayPageScroll;
}

function closeDiv(){
	document.getElementById('pbox_address').style.display='none';
	document.getElementById('fixbg').style.display='none';
}
	
	
	function save() {
		var defaultAddr = 0;
		if ($("#defaultAddr").attr("checked")) {
			defaultAddr = $("#defaultAddr").val();
		}
		//清空操作
		$("#person_msg").html("");
		$("#province_msg").html("");
		$("#address_msg").html("");
		$("#postcode_msg").html("");
		$("#phone_msg").html("");
		
		var person = $.trim($("#person").val());
		var province = $.trim($("#s1").val());
		var city = $.trim($("#s2").val());
		var street = $.trim($("#address").val());
		var postcode = $.trim($("#postcode").val());
		var phone = $.trim($("#phone").val());
		
		if(person==''){
			$("#person_msg").html("收货人姓名不能为空!");
			return;
		}
		
		if(person.length>20){
   			$("#person_msg").html("收货人姓名 不能超过20个字符!");
   			return;
   		}
		
		if(province=='请选择' || city == '请选择'){
			$("#province_msg").html("所在地区未选择!");
   			return;
		}
		
		if(street==''){
			$("#address_msg").html("街道地址不能为空!");
			return;
		}
		
		if(street.length>180){
   			$("#address_msg").html("街道地址 不能超过180个字符!");
   			return;
   		}
		
		if(!(postcode=='')){
			var reg =/^[0-9]{6}$/;
			//必须为6位数字
			if(!reg.test(postcode)){
				$("#postcode_msg").html("邮政编码格式错误!");
	   			return;
			}
		}
		
		if(phone==''){
			$("#phone_msg").html("联系电话不能为空!");
			return;
		}
		
		if(!(phone=='')){
			var reg1 =/^0{0,1}(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$/;
			var reg2 = new RegExp("^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$");
			
		  	if(!reg1.test(phone) && !reg2.test(phone)){
			  	$("#phone_msg").html("联系电话格式不正确!");
			  	return;
		  	}
		}
		$.ajax({
			url:'saveAddrForAjax.htm',
			type:'post',
			data:{"userAddr.person":person,"userAddr.street":street,"userAddr.phone":phone,"userAddr.postcode":postcode,"userAddr.province":province,"userAddr.city":city,"userAddr.defaultAddr":defaultAddr,"userAddr.userId":userId},
			success:function(json) {
				closeDiv();
				if (json.ajaxMsg != null) {
					alert(json.ajaxMsg);
				}
				else {
					refreshAddress(json.userAddr);
				}
			}
		});
		
	}

	function confirmOrder() {
		var values = $("[name=addrRd]:checked").val().split(",");

		$("#receivePerson").val(values[2]);
		$("#receivePhone").val(values[3]);
		$("#receiveAddress").val(values[4]);
		$("#receivePostcode").val(values[5]);
		$("#confirmOrder").submit();
	}
	
	function refreshAddress(userAddr) {
		var li = "<li id='mailTo" + userAddr.addrId + "' class='focus'><span>寄送至：</span>";
		var value = userAddr.addrId + "," + userAddr.userId + "," + userAddr.person + "," + userAddr.phone + "," + userAddr.address + "," + userAddr.postcode;
		li += "<input name='addrRd' type='radio' value='" + value + "' checked />";
		li += "<span class='span34'>" + userAddr.mailTo + "</span>";
		if (userAddr.defaultAddr == 1) {
			li += "<span>（默认地址）</span>";
			if (lastDefaultId != 0) {
				$("#mailTo" + lastDefaultId + " span:eq(2)").addClass("set");
				$("#mailTo" + lastDefaultId + " span:eq(2)").html("<a defaultValue='" + lastDefaultId + "," + userAddr.userId  + "' href='javascript:void(0)'>设置为默认收货地址</a>");	
			}
			lastDefaultId = userAddr.addrId;
		}
		else {
			li += "<span class='set'><a defaultValue='" + userAddr.addrId + "," + userAddr.userId + "' href='javascript:void(0)'>设置为默认收货地址</a></span>"; 
		}
		li += "</li>";
		$("#userAddrs").append(li);
		if (lastCheckId != 0) {
			$("#mailTo" + lastCheckId + " input").attr("checked", false);
			$("#mailTo" + lastCheckId).removeClass("focus");
			$("#mailTo" + lastCheckId + " span:eq(0)").html("");
			$("#mailTo" + lastCheckId + " span:eq(1)").removeClass("span34");	
		}
		lastCheckId = userAddr.addrId;
		
		if (!isLoad) {
			load();
			isLoad = true;
		}
	}

</script>
<s:include value="address.jsp" />